package com.lujin.eduorder.service.impl;

import com.lujin.commonutils.ordervo.CourseWebVoOrder;
import com.lujin.commonutils.ordervo.UcenterMemberOrder;
import com.lujin.eduorder.client.EduCLient;
import com.lujin.eduorder.client.UcenterClient;
import com.lujin.eduorder.entity.Order;
import com.lujin.eduorder.mapper.OrderMapper;
import com.lujin.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lujin.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lujin
 * @since 2021-07-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduCLient eduCLient;

    @Autowired
    UcenterClient ucenterClient;

    //1 生成订单方法
    @Override
    public String createOrders(String courseID, String memberId) {
        //通过远程调用，根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        //通过远程调用，根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduCLient.getCourseInfoOrder(courseID);

        //创建Order对象，想Order对象里面设置需要数据
        //添加到数据库
        Order order=new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseID);//课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());

        order.setStatus(0);//支付状态，支付后为1
        order.setPayType(1);//支付类型，微信1
        baseMapper.insert(order);

        //返回订单号
        return order.getOrderNo();
    }
}
